﻿using System;
using UnityEngine;
using System.Collections;

namespace Inlycat
{

    /// <summary>
    /// 截屏
    /// </summary>

    public class Screenshot
    {
        /// <summary>
        /// 全屏截图
        /// </summary>
        /// <param name="path">保存路径</param>
        public static void ShotByUnity(string path)
        {
            Application.CaptureScreenshot(path);
        }

        /// <summary>
        /// 截取某个范围图像
        /// </summary>
        /// <param name="rect">截取范围</param>
        /// <param name="path">保存路径</param>
        /// <param name="callBack">回调方法</param>
        /// <param name="imageType">截取图片格式</param>
        public static void ShotByRect(Rect rect, string path, Action<Texture2D> callBack = null, ScreenShotImageType imageType = ScreenShotImageType.JPG)
        {
            Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
            screenShot.ReadPixels(rect, 0, 0);
            screenShot.Apply();

            byte[] bytes = { };
            if (imageType == ScreenShotImageType.JPG)
            {
                bytes = screenShot.EncodeToJPG(100);
            }
            else if (imageType == ScreenShotImageType.PNG)
            {
                bytes = screenShot.EncodeToPNG();
            }

            System.IO.File.WriteAllBytes(path, bytes);
            Debug.Log(string.Format("Screenshot: {0}", path));

            if (callBack != null)
            {
                callBack(screenShot);
            }
        }

        /// <summary>
        /// 截取相机图像
        /// </summary>
        /// <param name="camera">相机</param>
        /// <param name="rect">截取范围</param>
        /// <param name="path">保存路径</param>
        /// <param name="callBack">回调方法</param>
        /// <param name="imageType">截取图片格式</param>
        public static void ShotVByCamera(Camera camera, Rect rect, string path, Action<Texture2D> callBack = null, ScreenShotImageType imageType = ScreenShotImageType.JPG)
        {
            RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
            camera.targetTexture = rt;
            camera.Render();

            RenderTexture.active = rt;
            Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
            screenShot.ReadPixels(rect, 0, 0);
            screenShot.Apply();

            camera.targetTexture = null;
            RenderTexture.active = null;
            GameObject.Destroy(rt);

            byte[] bytes = { };
            if (imageType == ScreenShotImageType.JPG)
            {
                bytes = screenShot.EncodeToJPG(100);
            }
            else if (imageType == ScreenShotImageType.PNG)
            {
                bytes = screenShot.EncodeToPNG();
            }

            System.IO.File.WriteAllBytes(path, bytes);
            Debug.Log(string.Format("Screenshot: {0}", path));

            if (callBack != null)
            {
                callBack(screenShot);
            }
        }

        public static void SaveTexture(Texture2D tex,string path,ScreenShotImageType format= ScreenShotImageType.PNG)
        {
            byte[] bytes = new byte[] {};
            switch (format)
            {
                case ScreenShotImageType.PNG:
                    bytes = tex.EncodeToPNG();
                    break;
                case ScreenShotImageType.JPG:
                    bytes = tex.EncodeToJPG();
                    break; 
            }
            Debug.Log(string.Format("Screenshot: {0}", path));
            System.IO.File.WriteAllBytes(path, bytes);
        }

        public enum ScreenShotImageType
        {
            PNG,
            JPG
        }
    }
}